#include "SaveGame.h"
#include "Land.h"
#include <iterator>
#include <iostream>

SaveGame::SaveGame(SaveGameInfor data, 
				   string objectPath ,
				   string inforPath)
{
	//_treePath=treePath;
	_objectPath=objectPath;
	_inforPath=inforPath;
	_data=data;
};


int SaveGame::getIDGameObject(EObjectType type)
{
	int typeObject=-1;
	switch (type)
	{
	case ECoin:
		typeObject = 2;
		break;
	case EQuestionBox_Flower:
		typeObject = 3;
		break;
	case EFungi:
		typeObject = 4;
		break;
	case EQuestionBox_Red:
		typeObject = 5;
		break;
	case EQuestionBox_Green:
		typeObject = 6;
		break;
	case EQuestionBox:
		typeObject = 7;
		break;
	case ETurtle:
		typeObject = 8;
		break;
	case EBreakBrick:
		typeObject = 9;
		break;
	case EFlowerMonster:
		typeObject = 10;
		break;
	case EQuestionBox_Star:
		typeObject = 11;
		break;	
	case EGoal:
		typeObject = 12;
		break;
	case EL_Brick:
		typeObject = 101;
		break;
	case EL_Block:
		typeObject = 102;
		break;
	case EL_Land1:
		typeObject = 103;
		break;
	case EL_Land2:
		typeObject = 104;
		break;
	case EL_Land3:
		typeObject = 105;
		break;
	case EL_PiPe:
		typeObject = 106;
		break;
	case EL_BarDown:
		typeObject = 107;
		break;
	case EL_BarUp:
		typeObject = 108;
		break;
	case EPipe_Down:
		typeObject = 109;
		break;
	case EPipe_Up:
		typeObject = 110;
		break;
	case EPipe_Right:
		typeObject = 111;
		break;
	case EPipe_BotTom:
		typeObject = 112;
		break;
	case ECloud:
		typeObject = 201;
		break;
	case EFence:
		typeObject = 202;
		break;
	case EGrass:
		typeObject = 203;
		break;
	case EMoutain:
		typeObject = 204;
		break;

	/*case EMario:
		typeObject = 301;
		break;
	case EMushroom_Red:
		typeObject = 302;
		break;
	case EMushroom_Green:
		typeObject = 303;
		break;
	case EStar:
		typeObject = 304;
		break;

	case EFlower:
		typeObject = 306;
		break;
	case EBullet:
		typeObject = 308;
		break;
	default:
		typeObject=305;
		break;*/
	}
	return typeObject;
};


void SaveGame::saveGameObjects()
{	
	ofstream fileObject(_objectPath);
	fileObject<<Convert(_data._tree->getRootNode()->getBound()._width)<<"\n";
	// save game objects
	CLoadMap *tmp=_data._cloadMap;
	map<int, GameObject*>::iterator it=tmp->mapGameObject.begin();
	
	for(it; it!=_data._cloadMap->mapGameObject.end(); ++it)
	{
		string gameObjectInfor="";		
		int id=it->first;
		GameObject* gameObject=it->second;

		if(gameObject==NULL)
			continue;
// 		if(gameObject->GetObjectState()==EState::Die)
// 			continue;

		// get real object type
		EObjectType type=gameObject->GetObjectType();
		if(type==ELand || type==EQuestionBox || type==EPipe || type==ESight)
			type=gameObject->GetRealObjectType();

		int typeObject= this->getIDGameObject(type);
		D3DXVECTOR2 pos=gameObject->GetPosition();
		gameObjectInfor+=Convert(id)+" "; // id order of each gameobject
		gameObjectInfor+=Convert(typeObject)+" "; // id to identify type of game object
		gameObjectInfor+=Convert(pos.x) +" ";		
		gameObjectInfor+=Convert(pos.y) +" ";
		gameObjectInfor+=Convert(gameObject->GetObjectState());

		// save gameobject infor to file
		fileObject<<gameObjectInfor<<"\n";
	}
	fileObject.close();
};

// save quadtree like format map editor
// addition information save in separate file: infor.txt
// when load this save, call CLoadMap as usual and read file infor.txt
// to get information about score, mario state, level....................
void SaveGame::Save()
{
	saveGameObjects();
	//saveTree();
	/////////////////////
	// write general infor 
	// level score life money
	// mario.x mario.y, mario.state
	// camera.x camera.y
	ofstream inforFile(_inforPath);
	inforFile<<Convert(_data._level)<<" ";
	inforFile<<Convert(_data._score)<<" ";
	inforFile<<Convert(_data._life)<<" ";
	inforFile<<Convert(_data._money)<<" ";
	inforFile<<"\n";
	// mario_position mario_state
	D3DXVECTOR2 pos=_data._mario->GetPosition();
	int state=_data._mario->GetMarioState();
	int preState = _data._mario->GetPreviousState();
	inforFile<<Convert(pos.x)<<" ";
	inforFile<<Convert(pos.y)<<" ";
	inforFile<<Convert(state)<<" ";
	inforFile<<Convert(preState)<<" ";
	inforFile<<"\n";
	inforFile.close();
}

